package cn.zhangxin.newyear.system.employee.controller;

import cn.zhangxin.newyear.system.employee.domain.EmployeeBO;
import cn.zhangxin.newyear.system.employee.service.IEmployeeService;
import cn.zhangxin.newyear.system.employee.domain.Employee;
import cn.zhangxin.newyear.system.employee.query.EmployeeQuery;
import cn.zhangxin.newyear.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    public IEmployeeService employeeService;


    /**
     * 保存和修改公用的
     * @param employee  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    @ApiOperation(value = "保存员工" )
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if( employee.getId()!=null)
                employeeService.update(employee);
            else
                employeeService.add(employee);
            return AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    @ApiOperation(value = "删除员工" )
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.delete(id);
            return AjaxResult.success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    @ApiOperation(value = "获取员工" )
    public Employee get(@PathVariable("id")Long id)
    {
        return employeeService.findOne(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    @ApiOperation(value = "获取全部员工" )
    public List<Employee> list(){

        return employeeService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    @ApiOperation(value = "高级查询员工" )
    public AjaxResult json(@RequestBody EmployeeQuery query)
    {
        try {
            List<Employee> byQuery = employeeService.findByQuery(query);
            Long total = employeeService.findByQueryCount(query);
            System.out.println(total);
            return AjaxResult.success().setData(byQuery).setTotal(total);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error();
    }


    /**
     *员工添加数据初始化管理员
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询员工全部数据")
    public AjaxResult findAll(){
        List<Employee> emps = employeeService.findAllBO();
        try {
            return AjaxResult.success().setData(emps);
        }catch (Exception e){
            e.printStackTrace();
        }
        return AjaxResult.error();
    }
}
